package jp.tkym.labs.swing3d.drawer;

import java.awt.Color;
import java.awt.Point;

import jp.tkym.labs.geom.Vector3f;
import jp.tkym.labs.m3d.AbstractModelConverter;
import jp.tkym.labs.m3d.ColorM3D;
import jp.tkym.labs.m3d.Line3D;
import jp.tkym.labs.m3d.Model3D;
import jp.tkym.labs.m3d.Rect3D;
import jp.tkym.labs.m3d.Text3D;

public class ModelConverterForSwing extends AbstractModelConverter{
	@Override
	public DrawG2D convert(Model3D model3d) {
		if(model3d instanceof Line3D) 
			return draw((Line3D) model3d);
		if(model3d instanceof Text3D) 
			return draw((Text3D) model3d);
		if(model3d instanceof Rect3D) 
			return draw((Rect3D) model3d);
		throw new IllegalArgumentException(
				"model is not supported type " 
				+ model3d.getClass().getName());
	}
	
	private Color colorOf(ColorM3D c){
		return new Color(c.r, c.g, c.b);
	}
	
	private Point pointOf(Vector3f v){
		return new Point(
				new Float(v.x).intValue(), 
				new Float(v.y).intValue());
	}
	
	private DrawText draw(Text3D model){
		Vector3f ts = translator.translate(model.p);
		return new DrawText(model.text, pointOf(ts), colorOf(model.getColor()));
	}
	
	private DrawLine draw(Line3D model){
		Vector3f ts = translator.translate(model.s);
		Vector3f te = translator.translate(model.e);
		return new DrawLine(pointOf(ts), pointOf(te), colorOf(model.getColor()));
	}
	
	private DrawPolygon draw(Rect3D model){
		Vector3f p00 = translator.translate(new Vector3f());
		Vector3f pw0 = translator.translate(new Vector3f(model.width, 0.0f, 0.0f));
		Vector3f p0h = translator.translate(new Vector3f(0.0f, model.height, 0.0f));
		Vector3f pwh = translator.translate(new Vector3f(model.width, model.height, 0.0f));
		return new DrawPolygon(
				colorOf(model.getColor()), 
				pointOf(p00),
				pointOf(pw0),
				pointOf(pwh),
				pointOf(p0h));
	}
	
//	private DrawRect draw(Rect3D model){
//		Vector3f p = translator.translate(new Vector3f());
//		Matrix_2_3 t = translator.affineTransform();
//		AffineTransform affine = new AffineTransform(t.m00, t.m10, t.m01, t.m11, t.m02, t.m12);
//		Dimension dim = new Dimension((int) model.width, (int) model.height);
//		return new DrawRect(pointOf(p), dim, colorOf(model.getColor()), affine);
//	}
}